package com.sljy.hzk.entity;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.sljy.hzk.net.CommonResult;
import com.sljy.hzk.net.HttpHeper;
import com.sljy.hzk.net.service.TaskService;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Response;

/**
 * Created  on 2018/7/6 0006
 *
 * @author 唐开阔
 * @describe 任务详情
 */
public class TaskDetailEntity {

    private List<Subitem> list;
    private String title;//标题
    private boolean isHaveAqyh = false;//是否存在安全隐患
    List<String> picPaths = new ArrayList<>();//已经选择的图片--文件地址
    List<String> picUrl = new ArrayList<>();//已经选择的图片--网络地址
    private String typeName;
    private int typeId;

    //同步上传图片并且赋值

    public void upPic(){
        picUrl.clear();
        for (int i = 0; i < picPaths.size(); i++) {
            File file = new File(picPaths.get(i));
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            MultipartBody.Part requestImgPart = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
            Response<CommonResult<String>> response = null;
            try {
                response = HttpHeper.get().create(TaskService.class).upPicture(requestImgPart).execute();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (response.isSuccessful() && response.body().getCode().equals("200") && !TextUtils.isEmpty(response.body().getData())) {
                picUrl.add(response.body().getData());
            }
        }
    }

    public List<Subitem> getList() {
        return list;
    }

    public String getTitle() {
        return title;
    }

    public List<String> getPicUrl() {
        return picUrl;
    }

    public void setPicUrl(List<String> picUrl) {
        this.picUrl = picUrl;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setList(List<Subitem> list) {
        this.list = list;
    }

    public boolean isHaveAqyh() {
        return isHaveAqyh;
    }

    public void setHaveAqyh(boolean haveAqyh) {
        isHaveAqyh = haveAqyh;
    }

    public List<String> getPicPaths() {
        return picPaths;
    }

    public void setPicPaths(List<String> picPaths) {
        this.picPaths = picPaths;
    }

    @Override
    public String toString() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }

    public static class Subitem {
        /**
         * array : ["男","女"]
         * defaultValue :
         * type : 2
         * required : 1
         */

        private Object defaultValue;
        private String type;
        private String required;
        private List<String> array;
        private List<Item> items;
        private String editStr; //输入的文字
        @Override
        public String toString() {
            Gson gson = new Gson();
            return gson.toJson(this);
        }
        public List<Item> getItems() {
            if (items == null) {
                items = new ArrayList<>();
                for (String s : array) {
                    items.add(new Item(s));
                }
            }
            return items;
        }

        /**
         * 是否为输入框类型
         * @return
         */
        public boolean isEditType(){
            return getType().equals("4") || getType().equals("5");
        }
        /**
         * 是否为选择类型
         * @return
         */
        public boolean isChoseType(){
            return getType().equals("2") || getType().equals("3");
        }

        /**
         * 是否有选项被选中
         * @return
         */
        public boolean isHaveChosed(){
            for (TaskDetailEntity.Subitem.Item item : getItems()) {
                if (item.isCheck()){
                    return true;
                }
            }
            return false;
        }

        public boolean isRequired(){
            return getRequired().equals("1");
        }
        public String getEditStr() {
            return editStr;
        }

        public void setEditStr(String editStr) {
            this.editStr = editStr;
        }

        public void setItems(List<Item> items) {
            this.items = items;
        }

        public String getDefaultValue() {
            String str = defaultValue.toString();
            if(str.equals(""))
                return str;
            String start = str.substring(0,1);
            String end = str.substring(str.length()-1,str.length());
            if(start.equals("[") & end.equals("]"))
                return str.substring(1,str.length()-1);
            return str;
        }

        public void setDefaultValue(String defaultValue) {
            this.defaultValue = defaultValue;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getRequired() {
            return required;
        }

        public void setRequired(String required) {
            this.required = required;
        }

        public List<String> getArray() {
            return array;
        }

        public void setArray(List<String> array) {
            this.array = array;
        }

        public static class Item {
            private String value;
            private boolean isCheck = false;
            @Override
            public String toString() {
                Gson gson = new Gson();
                return gson.toJson(this);
            }
            public Item(String value) {
                this.value = value;
            }

            public String getValue() {
                return value;
            }

            public void setValue(String value) {
                this.value = value;
            }

            public boolean isCheck() {
                return isCheck;
            }

            public void setCheck(boolean check) {
                isCheck = check;
            }
        }

    }
}
